package com.example.sys2.controller;


import com.example.sys2.common.result.Result;
import com.example.sys2.common.result.ResultUtils;
import com.example.sys2.entity.Cart;
import com.example.sys2.entity.vo.CartVo;
import com.example.sys2.service.ICartService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liruirui
 * @since 2023-09-21
 */
@RestController
@RequestMapping("/cart")
public class CartController {


    @Autowired
    ICartService service;
    @PostMapping("/add")
    public Result add(@RequestBody Cart cart) {
        boolean res = service.save(cart);
        if (res) {
            return (Result) ResultUtils.returnSuccess();
        }
        return (Result) ResultUtils.returnFail();
    }

    /**
     * 根据桌号获取购物车列表
     * @param cart
     * @return
     */
    @GetMapping("/getListByDesk")
    public Result getListByDesk(Cart cart) {
        List<CartVo> list = service.getListByDesk(cart.getDeskId());
        return ResultUtils.returnDataSuccess(list);
    }

    @GetMapping("/getCartList")
    public Object getCartList(){
        return ResultUtils.returnSuccessLayui(service.list(),service.list().size());
    }


    @PostMapping("/addCart")
    public Object addCart(Cart cart){
        if(service.save(cart)){
            return  ResultUtils.returnSuccess();
        }else{
            return  ResultUtils.returnFail();
        }
    }

    @PostMapping("/updateCart")
    public Object updateCart(Cart cart){
        if(service.updateById(cart)){
            return  ResultUtils.returnSuccess();
        }else{
            return  ResultUtils.returnFail();
        }
    }

    @PostMapping("/deleteCart")
    public Object deleteCart(Cart cart){
        if(service.removeById(cart.getId())){
            return  ResultUtils.returnSuccess();
        }else{
            return  ResultUtils.returnFail();
        }
    }


}